/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.lite.event;

import com.xsm.lite.util.ToStringBuilder;


/**
 * Events that represent the response to requests.
 * Subclasses should override and provide a way to obtain a result (e.g. getResult()).
 *      that returns the exact type of result specific to your request/response.
 * 
 * Please Note:
 * ----------- 
 * RequestEvent+ResponseEvent combo is equivalent to a method signature and as such 
 *  a ResponseEvent holding multiple results (or return values) would be conceptually invalid.
 * 
 * @author Sony Mathew
 */
public abstract class ResponseEvent extends Event {

    public enum Status {Success, Fail};
    
    private Status status = null;
    private String requestKey = null;
    
    /**
     * Not to be used - only for serialization purposes.
     * 
     * author Sony Mathew
     */
    public ResponseEvent() {        
    }
    
    /**
     * Represents a completion with the given status.
     * 
     * author Sony Mathew
     */
    public ResponseEvent(String requestKey, Status status) {
        this.requestKey = requestKey;
        this.status = status;        
    }

    /**
     * Obtain the status of completion.
     * 
     * author Sony Mathew
     */
    public Status getStatus() {
        return status;
    }

    /**
     * Shortcut for checking status.
     * 
     * author Sony Mathew
     */
    public boolean isFail() {
        return status == Status.Fail;
    }
    
    /**
     * Shortcut for checking status.
     * 
     * author Sony Mathew
     */
    public boolean isSuccess() {
        return status == Status.Success;
    }
    
    /**
     * A utilitiy to recursively goes into the cause to find the deepest underlying cause.
     * 
     * author Sony Mathew
     */
    public static Throwable getRootCause(Throwable cause) {
        while (cause.getCause() != null) {
            cause = cause.getCause();
        }
        return cause;
    }
    
    /**
     * Obtain the key to the request for which this is the response.
     * 
     * author Sony Mathew
     */
    public String getRequestKey() {
        return requestKey;
    }
    
    /**
     * Indicate whether the given event was the requestor.
     *  
     * author Sony Mathew
     */
    public boolean isRequestor(RequestEvent e) {
    	if(e == null) {
    		return false;
    	}
        return getRequestKey().equals(e.getKey());
    }
    
    /**
     * Indicate whether the given event was the NOT the requestor.
     *  
     * author Sony Mathew
     */
    public boolean isNotRequestor(RequestEvent e) {
        return !isRequestor(e);
    }
        
    /**
     * author Sony Mathew
     */
    public String toString() {
        return new ToStringBuilder(getClass())
            .append("requestKey", getRequestKey())
            .append("status", getStatus())
            .toString();
    }

}
